unit unDataSelect;

interface

uses
  Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, BusinessLogicInterface, DBGridEhGrouping, GridsEh, DBGridEh, cxGraphics, cxControls,
  cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit, dxSkinsCore,
  dxSkinBlack, dxSkinBlue, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinFoggy, dxSkinGlassOceans, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinPumpkin, dxSkinSeven,
  dxSkinSharp, dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinsDefaultPainters, dxSkinValentine, dxSkinXmas2008Blue, cxGroupBox, DB,
  ADODB, CommonInterface, StdCtrls;

type
  TfrmDataSelect = class(TForm, IDataSelForm)
    dbgMaster: TDBGridEh;
    dbgDetail: TDBGridEh;
    gbDetail: TcxGroupBox;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    qryMaster: TADOQuery;
    qryDetail: TADOQuery;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FDetailKeyField:string;
    FFillField:TStringList;
    FUseDetail:boolean;
    FDataSelectID:string;
    procedure SetUseDetail(const Value:boolean);
    procedure SetDataSelectID(const Value:string);
    procedure SetMasterQRYCMD(const Value:string);
    procedure SetDetailQRYCMD(const Value:string);
  public
    { Public declarations }
    function ShowDataSelForm(KeyWord: string):boolean;virtual;
    function GetDatabyDS(DS:TDataSource):boolean;virtual;
    function GetDatabyForm(F:TForm):boolean;virtual;
    function SetFillField(FieldList:string):boolean;virtual;
  public
    property UseDetail:boolean read FUseDetail write SetUseDetail default False;
    property DataSelectID :string read FDataSelectID write SetDataSelectID;
    property MasterQRYCMD:string write SetMasterQRYCMD;
    property DetailQRYCMD:string write SetDetailQRYCMD;
    property DetailKeyField:string read FDataSelectID write FDetailKeyField;
  end;

implementation

{$R *.dfm}

{ TfrmDataSelect }

procedure TfrmDataSelect.FormCreate(Sender: TObject);
begin
  Top:=trunc(screen.DesktopHeight / 2)-trunc(self.Height / 2);
  Left:=trunc(screen.DesktopWidth / 2)-trunc(self.Width / 2);

  qryMaster.Connection:=TADOConnection(SYSDB.GetDBConnection);
  qryDetail.Connection:=TADOConnection(SYSDB.GetDBConnection);

  FFillField:=TStringList.Create;
end;

function TfrmDataSelect.GetDatabyDS(DS:TDataSource): boolean;
var I:integer;
begin
  result:=True;
  for I := 0 to FFillField.Count - 1 do
    DS.DataSet.FieldByName(FFillField[I]).Value:=qryMaster.FieldByName(FFillField[I]).Value;
end;

function TfrmDataSelect.GetDatabyForm(F: TForm): boolean;
var I:integer;
    str:string;
begin
  for I := 0 to F.ControlCount - 1 do
  begin
    if (F.Controls[I] is TEdit) then
    begin
      str:=TEdit(F.Controls[I]).Name;
      if qryMaster.FindField(str)<>nil then
        TEdit(F.Controls[I]).Text:=qryMaster.FieldByName(str).AsString;
    end;
  end;
end;

procedure TfrmDataSelect.SetDataSelectID(const Value: string);
var Q:TADOQuery;
    fname,fchnname,fwidth:TStringList;
    I:integer;
    Col:TColumnEh;
begin
  FDataSelectID:=Value;

  fname:=TStringList.Create;
  fchnname:=TStringList.Create;
  fwidth:=TStringList.Create;

  Q:=TADOQuery.Create(nil);
  Q.Connection:=TADOConnection(SYSDB.GetDBConnection);
  Q.SQL.Add('select * from DBGridSet where FormID=:ID and DBGridName=:Name');
  Q.Parameters.ParamByName('ID').Value:=Value;
  Q.Parameters.ParamByName('Name').Value:='MASTER';
  Q.Open;
  if not Q.Eof then
  begin
    fname.CommaText:=Q.FieldByName('FieldList').AsString;
    fchnname.CommaText:=Q.FieldByName('FieldCHNName').AsString;
    fwidth.CommaText:=Q.FieldByName('FieldWidth').AsString;
  end;
  dbgMaster.Columns.Clear;
  for I := 0 to fname.Count - 1 do
  begin
    Col:=dbgMaster.Columns.Add;
    Col.FieldName:=fname[I];
    Col.Title.Caption:=fchnname[I];
    Col.Title.Alignment:=taCenter;
    Col.Width:=strtoint(fwidth[I]);
  end;

  if FUseDetail then
  begin
    Q.SQL.Add('select * from DBGridSet where FormID=:ID and DBGridName=:Name');
    Q.Parameters.ParamByName('ID').Value:=Value;
    Q.Parameters.ParamByName('Name').Value:='Detail';
    Q.Open;
    if not Q.Eof then
    begin
      fname.CommaText:=Q.FieldByName('FieldList').AsString;
      fchnname.CommaText:=Q.FieldByName('FieldCHNName').AsString;
      fwidth.CommaText:=Q.FieldByName('FieldWidth').AsString;
    end;
    dbgDetail.Columns.Clear;
    for I := 0 to fname.Count - 1 do
    begin
      Col:=dbgDetail.Columns.Add;
      Col.FieldName:=fname[I];
      Col.Title.Caption:=fchnname[I];
      Col.Title.Alignment:=taCenter;
      Col.Width:=strtoint(fwidth[I]);
    end;
  end;

  FreeAndNil(fname);
  FreeAndNil(fchnname);
  FreeAndNil(fwidth);
  FreeAndNil(Q);
end;

procedure TfrmDataSelect.SetDetailQRYCMD(const Value: string);
begin
  qryDetail.Close;
  qryDetail.SQL.Clear;
  qryDetail.SQL.Add(Value);
end;

function TfrmDataSelect.SetFillField(FieldList: string): boolean;
begin
  FFillField.CommaText:=FieldList;
end;

procedure TfrmDataSelect.SetMasterQRYCMD(const Value: string);
begin
  qryMaster.Close;
  qryMaster.SQL.Clear;
  qryMaster.SQL.Add(Value);
end;

procedure TfrmDataSelect.SetUseDetail(const Value: boolean);
begin
  gbDetail.Visible:=Value;
end;

function TfrmDataSelect.ShowDataSelForm(KeyWord: string): boolean;
begin
  result:=False;
  qryMaster.Close;
  qryMaster.Parameters.ParamByName('Type').Value:=FDataSelectID;
  qryMaster.Parameters.ParamByName('Key').Value:=KeyWord;
  qryMaster.Open;
  if qryMaster.RecordCount=0 then
    exit;
  result:=True;
  if qryMaster.RecordCount>1 then
    ShowModal;
end;

end.
